<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/9/2
 * Time: 15:13
 */
namespace App\Services;

use App\Repositories\RepaymentPlanRepository;
use App\Repositories\UserLevelRelationsRepository;
use App\Repositories\UserProfitRepository;
use App\Repositories\UserRepository;

class InvitedService extends CommonService
{
    protected $userRepository;
    protected $userLevelRelationsRepository;
    protected $userProfitRepository;
    protected $repaymentPlanRepository;

    public function __construct(UserRepository $userRepository,
                                UserLevelRelationsRepository $userLevelRelationsRepository,
                                UserProfitRepository $userProfitRepository,
                                RepaymentPlanRepository $repaymentPlanRepository)
    {
        $this->userRepository = $userRepository;
        $this->userLevelRelationsRepository = $userLevelRelationsRepository;
        $this->userProfitRepository = $userProfitRepository;
        $this->repaymentPlanRepository = $repaymentPlanRepository;
    }

    /**
     * 邀请人数统计
     * @param $userId
     * @return \Illuminate\Http\JsonResponse
     */
    public function invitedNumber($userId)
    {
        $data = $this->invitedCount($userId);
        return $this->responseJson(0, $data);
    }

    public function invitedCount($userId, $isTime = false, $startTime = 0, $endTime = 0)
    {
        $ownRelations = $this->userLevelRelationsRepository->first($userId);
        //直接邀请人数
        if (!$isTime) {
            $directInvitedNumber = $this->userLevelRelationsRepository->getUserLevelRelationsByPidCount($ownRelations->id);
        } else {
            $directInvitedNumber = $this->userLevelRelationsRepository->getUserLevelRelationsByPidCountByCreateTime($ownRelations->id, $startTime, $endTime);
        }
        //获取所有下级
        $getAllDownLevel = $this->userLevelRelationsRepository->getUserLevelRelationsByPid($ownRelations->id);
        //间接邀请人数
        $indirectInvitedNumber = [];
        if (!empty($getAllDownLevel)) {
            foreach ($getAllDownLevel as $key=>$value) {
                if (!$isTime) {
                    $indirectInvitedNumber[$key] = $this->userLevelRelationsRepository->getUserLevelRelationsByPidCount($value['id']);
                } else {
                    $indirectInvitedNumber[$key] = $this->userLevelRelationsRepository->getUserLevelRelationsByPidCountByCreateTime($value['id'], $startTime, $endTime);
                }
            }
        }
        //总收益
        $profitAll = $this->userProfitRepository->profitAll($userId);
        $profitAllToday = $this->userProfitRepository->profitFromTypeToday($userId);
        $profitAllYesterday = $this->userProfitRepository->profitFromTypeYesterday($userId);
        $profitAllThisMonth = $this->userProfitRepository->profitFromTypeThisMonth($userId);
        $indirectInvitedNumber = array_sum($indirectInvitedNumber);
        $data = [
            'direct_invited_number' => $directInvitedNumber,
            'indirect_invited_number' => $indirectInvitedNumber,
            'profit_all' => $profitAll,
            'profit_all_today' => $profitAllToday,
            'profit_all_yesterday' => $profitAllYesterday,
            'profit_all_this_month' => $profitAllThisMonth,
        ];
        return $data;
    }

    /**
     * 被邀请人列表
     * @param $userId
     * @param $level
     * @return \Illuminate\Http\JsonResponse
     */
    public function directBeInvited($userId, $level, $page)
    {
        $ownRelations = $this->userLevelRelationsRepository->first($userId);
        if (0 === $level) {
            //直接邀请
            $directInvited = $this->userLevelRelationsRepository->getUserLevelRelationsByPidPaginate($ownRelations->id, $page, 10);
        } else {
            //直接邀请
            $directInvited = $this->userLevelRelationsRepository->getUserLevelRelationsByPidAndLevelPaginate($ownRelations->id, $level, $page, 10);
        }
        $data = [];
        if (!empty($directInvited)) {
            foreach ($directInvited as $key=>$value) {
                $data[$key] = $value;
                $beInvitedUser = $this->userRepository->byId($value['user_id']);
                $data[$key]['realname'] = isset($beInvitedUser->realname) ? hiddenRealName($beInvitedUser->realname) : '';
                $data[$key]['mobile'] = isset($beInvitedUser->mobile) ? hiddenMobile($beInvitedUser->mobile) : '';
                $data[$key]['head_pic'] = isset($beInvitedUser->head_pic) ? getOssImageUrl($beInvitedUser->head_pic) : '';
            }
        }
        return $this->responseJson(0, $data);
    }

    public function directBeInvitedDetail($id, $userId)
    {
        $relation = $this->userLevelRelationsRepository->byId($id);
        if (empty($relation)) {
            return $this->responseJson(13000);//该邀请人不存在
        }
        $directInvitedNumber = $this->userLevelRelationsRepository->getUserLevelRelationsByPidCount($relation->id);
        $profitToYou = $this->userProfitRepository->profitAllToUser($relation->user_id, $userId);
        $isUsePlan = $this->repaymentPlanRepository->isUsePlan($relation->user_id);
        if (!empty($isUsePlan)) {
            $isUseRepayment = 1;
        } else {
            $isUseRepayment = 0;
        }
        $data = [
            'direct_invited_number' => $directInvitedNumber,
            'profit_to_you' => $profitToYou,
            'is_use' => $isUseRepayment,
        ];
        return $this->responseJson(0, $data);
    }
}